using UnityEngine;
using System.Collections;
using System.Collections.Generic;


[RequireComponent(typeof(Collider))]
public class CounterTrigger : CharacterAction
{	
    public List<Character> hitten = new List<Character>(); 
    public Collider colliderRef;
    public Transform refTrans;

	public override void Init(CharacterKeyFrame frame)
	{
		base.Init(frame);

		colliderRef = collider;
		colliderRef.enabled = false;
		colliderRef.isTrigger = true;

	}

	public override void OnActionEnter (Character character)
	{
		base.OnActionEnter (character);
		colliderRef.enabled = true;
		hitten.Clear();
		character.rigidbodyRef.WakeUp();  
    }

	public override void OnActionExit (Character character)
	{
		base.OnActionExit (character);
		colliderRef.enabled = false;

		foreach(Character receiver in hitten)
		{
			receiver.counterCharacters.Remove(character);
		}
	}

	void OnTriggerEnter(Collider other) 
	{ 
		if(other.isTrigger == false)
		{  
			Character receiver = other.GetComponent<Character>();
			if(receiver == null || hitten.Contains(receiver))
            {
				return;
            }
			if(receiver.gameObject.layer == character.gameObject.layer)//enemy in same layer means no damage
			{
				return;
			}

			if(receiver.counterCharacters.Contains(character) == false)
			{
				receiver.counterCharacters.Add(character);
				hitten.Add(receiver);
			}
		}
	}

	void OnTriggerExit(Collider other)
	{
		if(other.isTrigger == false)
		{  
			Character receiver = other.GetComponent<Character>();
			if(receiver == null || hitten.Contains(receiver))
			{
				return;
			}
			if(receiver.gameObject.layer == character.gameObject.layer)//enemy in same layer means no damage
			{
				return;
			}

			receiver.counterCharacters.Remove(character);
			hitten.Remove(receiver);
		}
	}
}

